<?php
/**
 * 
 * @author tangbl
 * 
 */
class ReplyModel extends Model {
	//自动验证文章内容
	protected $_validate=array(
		array('retype','require','{%rpl_form_valid_retype_req}'),
	);
	
	//自动填充内容  addtime
	protected $_auto   =   array(

	);
	
	//根据回复标识找到对应的回复记录
	public function replyBySolevar($reply_solevar){
		return $this->where("reply_solevar='$reply_solevar'")->find();
	}
	
	//根据内容的solevar 判断该内容的其他事件类型是否已经存在
	public function validByReplyType(){
		$where  = 'reply_type>3';
		$types  = $this->field("reply_type")->where($where)->select();
		$rows   = array();
		foreach($types as $v){
			$rows[] = $v['reply_type'];
		}
		return $rows;
	}
	
	//根据关键字数组查找reply是否存在对应关键字记录
	public function getReplyByKey($keys){
		$where = "";
		foreach($keys as $k=>$v){
			if($k){
				$where .= ",'".$v."'";
			}else{
				$where .= "'".$v."'";	
			}
		}
		$where  = "keywords in($where)";
		$lists  = $this->field("reply_solevar,keywords")->where($where)->select();
		$rows   = array();
		foreach($lists as $v){
			$rows[$v['keywords']] = $v['reply_solevar']	;
		}
		return $rows;
	}
	
	
	//根据事件类型判断该类型是否存在
	public function getReplyByType($type){
		$where = "reply_type=$type";
		return   $this->where($where)->getField("id");
	}
	
	//初始化编辑回复表单
	public function emptyReply($data){
		$arrays = array('event_type','event_value','location_lx','location_ly',
						'location_rx','location_ry','keywords');
		foreach($arrays as $v){
			$data[$v] = '';	
		}
		return $data;
	}
	
	//根据查询数组获取一条记录
	public function getReplyRowlyByMap($map){
		return $this->where($map)->find();
	}
	
	//根据地理位置找到对应的回复记录
	public function getReplyRowByLocation($map,$data){
		$lists = $this->where($map)->select();
		$x = floatval($data['Location_X']);
		$y = floatval($data['Location_Y']);
		
		$default = array();
		$nowrows = array();
		foreach($lists as $v){
			$rx = floatval($v['location_rx']);
			$lx = floatval($v['location_lx']);
			
			$ry = floatval($v['location_ry']);
			$ly = floatval($v['location_ly']);
		
			if( ($x>$rx) && ($x<$lx) && ($y<$ry) && ($y>$ly) ){
				$nowrows = $v;break;
			}
			if($v['islocation'] == 1){
				$default = $v;
			}
		}
		if(!$nowrows){
			$nowrows = $default;	
		}
		return $nowrows;
	}
	
	
	
}
?>